/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package qs.crm.modules.client.cstLost.service.impl;

import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import qs.crm.modules.client.cstLost.domain.CstLost;
import qs.crm.modules.client.cstLost.repository.CstLostRepository;
import qs.crm.modules.client.cstLost.service.CstLostService;
import qs.crm.modules.client.cstLost.service.dto.CstLostDto;
import qs.crm.modules.client.cstLost.service.dto.CstLostQueryCriteria;
import qs.crm.modules.client.cstLost.service.mapstruct.CstLostMapper;
import qs.crm.utils.FileUtil;
import qs.crm.utils.PageUtil;
import qs.crm.utils.QueryHelp;
import qs.crm.utils.ValidationUtil;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
* @website https://eladmin.vip
* @description 服务实现
* @author tong
* @date 2023-06-02
**/
@Service
@RequiredArgsConstructor
public class CstLostServiceImpl implements CstLostService {

    private final CstLostRepository cstLostRepository;
    private final CstLostMapper cstLostMapper;

    @Override
    public Map<String,Object> queryAll(CstLostQueryCriteria criteria, Pageable pageable){
        Page<CstLost> page = cstLostRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(cstLostMapper::toDto));
    }

    @Override
    public List<CstLostDto> queryAll(CstLostQueryCriteria criteria){
        return cstLostMapper.toDto(cstLostRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public CstLostDto findById(Long lstId) {
        CstLost cstLost = cstLostRepository.findById(lstId).orElseGet(CstLost::new);
        ValidationUtil.isNull(cstLost.getLstId(),"CstLost","lstId",lstId);
        return cstLostMapper.toDto(cstLost);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public CstLostDto create(CstLost resources) {
        return cstLostMapper.toDto(cstLostRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(CstLost resources) {
        CstLost cstLost = cstLostRepository.findById(resources.getLstId()).orElseGet(CstLost::new);
        ValidationUtil.isNull( cstLost.getLstId(),"CstLost","id",resources.getLstId());
        cstLost.copy(resources);
        cstLostRepository.save(cstLost);
    }

    @Override
    public void deleteAll(Long[] ids) {
        for (Long lstId : ids) {
            cstLostRepository.deleteById(lstId);
        }
    }

    @Override
    public void download(List<CstLostDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (CstLostDto cstLost : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("客户编号", cstLost.getLstCustNo());
            map.put("客户姓名", cstLost.getLstCustName());
            map.put("客户经理编号", cstLost.getLstCustManagerId());
            map.put("客户经理姓名", cstLost.getLstCustManagerName());
            map.put("最后下单时间", cstLost.getLstLastOrderDate());
            map.put("确认流失时间", cstLost.getLstLostDate());
            map.put("暂缓措施", cstLost.getLstDelay());
            map.put("流失原因", cstLost.getLstReason());
            map.put("状态", cstLost.getLstStatus());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}